﻿using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using UIShell.OSGi;
using Pasys.PresentationCore.Utility;

namespace Pasys.PresentationCore.WinForms
{
    public class ToolStripStuffer : CatagorizedBuilder<ToolStrip>
    {
        private ToolStrip _toolStrip = null;
        public ToolStripStuffer(ToolStrip toolStrip)
            : base()
        {
            this._toolStrip = toolStrip;
            var id = base.DefaultCatagory;
            var statusMonitor = new ToolbarStatusMonitor(id,
                                                              visible => toolStrip.Visible = visible);
        }

        public override void Build(XmlNode xmlNode, IBundle owner)
        {
            ToolStrip toolStrip = _toolStrip;
            if (toolStrip == null)
            {
                return;
            }

            if (xmlNode.Name == "ToolbarItem")
            {
                ToolStripItem item = ToolbarItemBuilder.Build(xmlNode, owner);
                ControlUtility.Invoke(toolStrip, () => toolStrip.Items.Add(item));
            }
            else
            {
                List<ToolStripItem> items = ToolbarItemBuilder.Build(xmlNode.ChildNodes, owner);
                ControlUtility.Invoke(toolStrip, () => toolStrip.Items.AddRange(items.ToArray()));
            }
        }
    }
}
